package surreal.triviamaster.gui;

import surreal.triviamaster.R;
import surreal.triviamaster.database.LocalCache;
import surreal.triviamaster.utilities.TriviaMasterApplication;
import surreal.triviamaster.utilities.Utilities;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

public class FlagAnswer extends Activity{
	private String yourAnswer="";
	private boolean isQuizOver=false;

	@Override
	  public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.flag_answer);
	    
	  //handles changing orientation
	    final Bundle data = (Bundle) getLastNonConfigurationInstance();
    	if(data!=null) {
    		yourAnswer=data.getString("yourAnswer");
    		isQuizOver=data.getBoolean("isQuizOver");
    	} else {
    		yourAnswer=getIntent().getExtras().getString("yourAnswer");
    		isQuizOver = getIntent().getExtras().getBoolean("isQuizOver");
    	}
	    
	    //Submit button
	    Button submit = (Button) findViewById(R.id.submit);
	    submit.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	String o =((TextView) findViewById(R.id.other)).getText().toString();
            	if(Utilities.isValidToUpload(o)) {
	            	//these two represent boolean values to be placed into local cache
	            	int listedFlag = 0;
	            	int mineCorrectFlag =0;
	            	if(((CheckBox) findViewById(R.id.listedIncorrect)).isChecked()) {
	            		listedFlag=1;
	            	}
	            	if(((CheckBox) findViewById(R.id.mineCorrect)).isChecked()) {
	            		mineCorrectFlag=1;
	            	}
	            	
	            	//send flag to local cache
	        	    LocalCache db = ((TriviaMasterApplication) getApplication()).getLocalCache();
	        	    db.flagCurrentQuestion(listedFlag, mineCorrectFlag, 0,0,yourAnswer, o);
	            	
	        		//Send user back to the game
	        		Intent intent = new Intent(FlagAnswer.this, Game.class);
		  	      	startActivity(intent);
            	} else {
            		Toast.makeText(FlagAnswer.this, TriviaMasterApplication.ERROR_INVALID_CHARACTERS, 10000).show();

            	}
            }
        });
	    
	    //Cancel button
	    Button cancel = (Button) findViewById(R.id.cancel);
	    cancel.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Intent intent = new Intent(FlagAnswer.this, Incorrect.class);
            	intent.putExtra("yourAnswer", yourAnswer);
            	intent.putExtra("isQuizOver", isQuizOver);
	  	      	startActivity(intent);
            }
        });
	}
	
	@Override
	public Bundle onRetainNonConfigurationInstance() {
		Bundle bundle = new Bundle();
		bundle.putString("yourAnswer", yourAnswer);
		bundle.putBoolean("isQuizOver", isQuizOver);
	    return bundle;
	}
	
}
